package com.nb.hdj.testCase.loginModule;

import com.nb.hdj.baseClass.SeleniumUtil;
import com.nb.hdj.util.YamlReadUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Description: TODO
 * @Author: hedongjia
 * @Date: 2021/11/5 下午6:29
 * @Version: v1.0
 */
public class CheckLoginCase {
    private static final Logger logger = LoggerFactory.getLogger(CheckLoginCase.class);
    YamlReadUtils yamlReadUtils=new YamlReadUtils();
    SeleniumUtil seleniumUtil=new SeleniumUtil();
    public Map<String, String> loginMap=yamlReadUtils.getMapValueByKey("Login", "htlogin.yaml");
    String userNameElement=loginMap.get("userNameElement");
    String passWordElement=loginMap.get("passWordElement");
    String yzmElement=loginMap.get("yzmElement");
    String loginButtonElement=loginMap.get("loginButtonElement");
    String tanKuang=loginMap.get("tankuang");
    /**
     * 打开aim-test2-service网址
     */
    public boolean openAimServiceUrl(WebDriver driver){
        //loginMap = yamlReadUtils.getMapValueByKey("Login", "htlogin.yaml");
        //yamlReadUtils.getMapValueByKey("Login", "htlogin.yaml");
        try{
            String loginUrl =loginMap.get("url");
            driver.get(loginUrl);
            logger.info("------登录页正常------");
            return true;
        }catch (Exception e){
            e.fillInStackTrace();
            logger.error("------登录页报错------{}",e.getMessage());
            return false;
        }
    }

    /**
     * 检查登录提示标题
     * @param driver
     * @return
     */
    public boolean LoginIframeCheck(WebDriver driver) {
        String  loginTitleElement= loginMap.get("loginTitleElement");
        String  loginTitle=loginMap.get("loginTitle");
        boolean check= seleniumUtil.elementTextIsEqual(driver, By.xpath(loginTitleElement),loginTitle);
        return check;
    }

    /**
     * 登录输入正确用户名 密码 验证码
     */
    public boolean InputNameAndPwdLogin(WebDriver driver) throws InterruptedException {
        String userNameElement=loginMap.get("userNameElement");
        String passWordElement=loginMap.get("passWordElement");
        String yzmElement=loginMap.get("yzmElement");
        String loginButtonElement=loginMap.get("loginButtonElement");
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(userNameElement),loginMap.get("username"));
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(passWordElement),loginMap.get("password"));
        //点击验证码
        seleniumUtil.elementClick(driver,By.xpath(loginMap.get("yzmButtonElement")));
        Thread.sleep(300);
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(yzmElement),loginMap.get("yzm"));
        seleniumUtil.elementClick(driver,By.xpath(loginButtonElement));
        if(seleniumUtil.elementTextIsEqual(driver,By.xpath(loginMap.get("shouYeTabsElement")),loginMap.get("shouYeTabs"))){
            return true;
        }
        return false;
    }

    /**
     * 登录输入错误用户名 正确密码 验证码
     */
    public boolean InputFailNameAndPwdLogin(WebDriver driver,String username,String password,String yzm) throws InterruptedException {
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(userNameElement),username);
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(passWordElement),password);
        seleniumUtil.elementClickAndClearAndInput(driver,By.xpath(yzmElement),yzm);
        seleniumUtil.elementClick(driver,By.xpath(loginButtonElement));
        if(seleniumUtil.alertText(tanKuang,"登录失败",driver)){
            return true;
        }
        return false;
    }

    /**
     * 登录事件用例
     */
    public boolean LoginCase(WebDriver driver) throws InterruptedException {
        if(openAimServiceUrl(driver)){
            if(LoginIframeCheck(driver)){
                InputNameAndPwdLogin(driver);
                return true;
            }
        }
        return false;
    }

    /**
     * 错误账号 登录时间用例
     */
    public boolean LoginFailNameCase(WebDriver driver,String username,String password,String yzm) throws InterruptedException {
        if(openAimServiceUrl(driver)){
            if(LoginIframeCheck(driver)){
                InputFailNameAndPwdLogin(driver,username,password,yzm);
                return true;
            }
        }
        return false;
    }

}
